package com.nand.module.function.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.XPopup;
import com.nand.module.function.R;
import com.nand.module.function.bean.SpareParBean;

import java.util.ArrayList;
import java.util.List;

/**
 * created by wzhao on 2020/3/24
 * describe:
 */
public class SpAdapter extends RecyclerView.Adapter<SpAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<SpareParBean> mList = new ArrayList<>();
    private String cont;

    private SpAdapter.OnItemClickListener mOnItemClickListener = null;
    private OnLongClickListener mLongClickListener;

    public SpAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<SpareParBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    public List<SpareParBean> getSpareBean() {
        return mList;
    }


    @Override
    public SpAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_sp, viewGroup, false);
        layout.setOnClickListener(this);
        return new SpAdapter.ViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(SpAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        SpareParBean spareParBean = mList.get(position);
        int a = position+1;
        viewHolder.textView.setText(a +"、"+spareParBean.getSpName());
        viewHolder.editText.setText("");
        viewHolder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                cont = s.toString();
                mList.get(position).setSpAmount(cont);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        // 必须在事件发生前，调用这个方法来监视View的触摸
        final XPopup.Builder builder = new XPopup.Builder(mContext)
                .watchView(viewHolder.itemView);
        if (mLongClickListener != null){
            final int finalPosition = position;
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    return mLongClickListener.onLongClick(builder,finalPosition);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        EditText editText;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textview);
            editText = itemView.findViewById(R.id.edittext);


        }

    }

    //两次点击按钮之间的间隔，目前为1000ms
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;

    public static interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }
    }

    public void setOnItemClickListener(SpAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setLongClickListener(OnLongClickListener longClickListener) {
        this.mLongClickListener = longClickListener;
    }

    public interface OnLongClickListener{
        boolean onLongClick(XPopup.Builder builder,int position);
    }

}


